import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    TextInput,
    TouchableOpacity,
    DeviceEventEmitter,
    ScrollView,
    Easing,
    Animated,
    Alert
} from 'react-native';

import Barcode from 'react-native-smart-barcode'

import Constant from '../Constant/Constant'
import ScreenUtils from '../Utils/ScreenUtils'
import ColorUtils from '../Utils/ColorUtils'
import ToastUtils from '../Utils/ToastUtils'
import HttpUtils from '../Utils/HttpUtils'
import RuleUtils from '../Utils/RuleUtils'
import EncryptUtils from '../Utils/EncryptUtils'
import RouterUtils from '../Utils/RouterUtils'
import StorageUtils from '../Utils/StorageUtils'
import TimeUtils from '../Utils/TimeUtils'

let that = null;
export default class QRCodeScan extends React.Component
{
    static navigationOptions = ({navigation}) => ({
        gesturesEnabled: false,//是否支持滑动返回收拾，iOS默认支持，安卓默认关闭
        headerTitle: '扫一扫',
        headerStyle: {
            backgroundColor: ColorUtils.white,
            height: ScreenUtils.statusBarHeight,
        },
        headerRight: (<Text></Text>),
        headerTintColor: ColorUtils.black,
        headerTitleStyle: {
            fontSize: ScreenUtils.Font_18,
            color: ColorUtils.black,
            fontWeight: 'normal',
            flex: 1,
            alignSelf: 'center',
            textAlign: 'center',
        }
    });

    constructor(props)
    {
        super(props);
        that = this;
        this.state = {};
    }

    componentDidMount()
    {
        RouterUtils.pushRouter('QRCodeScan', this.props.navigation.state.key);
    }

    componentWillUnmount()
    {
        RouterUtils.popRouter('QRCodeScan', this.props.navigation.state.key);
    }

    onBarCodeRead(e)
    {
        that.stopScan();
        ToastUtils.toastShortBottom('扫码成功');
        DeviceEventEmitter.emit('callbackScan', e.nativeEvent.data.code);
        that.props.navigation.goBack();
    }

    stopScan()
    {
        this._barCode.stopScan()
    }

    render()
    {
        return (
            <View style={styles.container}>
                <Barcode style={{flex: 1,}} ref={component => this._barCode = component} onBarCodeRead={this.onBarCodeRead}/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: ColorUtils.bgColor,
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
});


